//
//  POIModelDataSource.swift
//  travelMapMvvm-swift2
//
//  Created by green on 15/11/4.
//  Copyright © 2015年 travelMapMvvm-swift2. All rights reserved.
//

import Foundation
import ReactiveCocoa
import AFNetworking
import TMCommonLib
import JSONHelper

public class POIModelDataSource: POIModelDataSourceProtocol {
    
    public class func shareInstance()->POIModelDataSourceProtocol{
        struct YRSingleton{
            static var predicate:dispatch_once_t = 0
            static var instance:POIModelDataSource? = nil
        }
        dispatch_once(&YRSingleton.predicate,{
            YRSingleton.instance=POIModelDataSource()
        })
        return YRSingleton.instance!
    }
    
    // MARK: - 查询攻略POI
    
    public func queryPOIListByStrategyId(poiTypeCodes:[String]?,strategyId:String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber:RACSubscriber!) -> RACDisposable! in
            
            var parameters: [String:AnyObject] = ["articleId":strategyId]
            
            if let poiTypeCodes=poiTypeCodes where poiTypeCodes.count != 0 {
                
                parameters["poiTypeCodes"] = covertToString(poiTypeCodes)
            }
            
            let manager = AFHTTPRequestOperationManager()
            manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html","text/plain"]) as Set<NSObject>
            
            manager.GET(kURL+"getArticlePOIs", parameters: parameters, success: { (operation, responseObj) -> Void in
                
                let resultDic = responseObj as! [NSObject:AnyObject]
                
                let success = resultDic[kSuccess] as? Int
                let msg     = resultDic[kMsg] as? String
                
                if success == 0 {
                    
                    var poiList:[POIModel]?
                    
                    
                    poiList <-- resultDic[kData]
                    
                    subscriber.sendNext(poiList)
                    subscriber.sendCompleted()
                    
                    
                } else {
                    
                    subscriber.sendError(DataError.QueryPOIListByStrategyId(msg).error)
                }
            
            }) { (operation, error) -> Void in
                
                subscriber.sendError(DataError.QueryPOIListByStrategyId(nil).error)
            }
            
            return nil
        })
    }
    
    // MARK: - 查询城市POI
    
    public func queryPOIListByCityId(pageIndex: Int, pageSize: Int, poiTypeCodes: [String]?, cityId: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber:RACSubscriber!) -> RACDisposable! in
            
            var parameters: [String:AnyObject] = ["pageIndex":pageIndex,"pageSize":pageSize,"destination":cityId]
            
            if let poiTypeCodes=poiTypeCodes where poiTypeCodes.count != 0 {
                
                parameters["poiTypeCodes"] = covertToString(poiTypeCodes)
            }
            
            let manager = AFHTTPRequestOperationManager()
            manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html","text/plain"]) as Set<NSObject>
            
            manager.GET(kURL+"getDestinationPOIs", parameters: parameters, success: { (operation, responseObj) -> Void in
                
                let resultDic = responseObj as! [NSObject:AnyObject]
                
                let success = resultDic[kSuccess] as? Int
                let msg     = resultDic[kMsg] as? String
                
                if success == 0 {
                    
                    var poiList:[POIModel]?
                    
                    poiList <-- resultDic[kData]
                    
                    subscriber.sendNext(poiList)
                    subscriber.sendCompleted()
                    
                } else {
                    
                    subscriber.sendError(DataError.QueryPOIListByCityId(msg).error)
                }
                
            }) { (operation, error) -> Void in
                
                subscriber.sendError(DataError.QueryPOIListByCityId(nil).error)
            }
            
            return nil
        })
    }
    
    // MARK: - 查询关键字POI
    
    public func queryPOIListByKeyword(pageIndex: Int, pageSize: Int, poiTypeCodes: [String]?, keyword: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber:RACSubscriber!) -> RACDisposable! in
            
            var parameters: [String:AnyObject] = ["pageIndex":pageIndex,"pageSize":pageSize,"keyword":keyword]
            
            if let poiTypeCodes=poiTypeCodes where poiTypeCodes.count != 0 {
                
                parameters["poiTypeCodes"] = covertToString(poiTypeCodes)
            }
            
            let manager = AFHTTPRequestOperationManager()
            manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html","text/plain"]) as Set<NSObject>
            
            manager.GET(kURL+"getPOIByKeyword", parameters: parameters, success: { (operation, responseObj) -> Void in
                
                let resultDic = responseObj as! [NSObject:AnyObject]
                
                let success = resultDic[kSuccess] as? Int
                let msg     = resultDic[kMsg] as? String
                
                if success == 0 {
                    
                    var poiList:[POIModel]?
                    
                    poiList <-- resultDic[kData]
                    
                    subscriber.sendNext(poiList)
                    subscriber.sendCompleted()
                    
                } else {
                    
                    subscriber.sendError(DataError.QueryPOIListByKeyword(msg).error)
                }
                
                }) { (operation, error) -> Void in
                    
                    subscriber.sendError(DataError.QueryPOIListByKeyword(nil).error)
            }
            
            return nil
        })
    }
    
    // MARK: - 查询已收藏的POI
    
    public func queryPOIListByUserId(pageIndex: Int, pageSize: Int, poiTypeCodes: [String]?, userId: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber:RACSubscriber!) -> RACDisposable! in
            
            var parameters: [String:AnyObject] = ["pageIndex":pageIndex,"pageSize":pageSize,"userId":userId]
            
            if let poiTypeCodes=poiTypeCodes where poiTypeCodes.count != 0 {
                
                parameters["poiTypeCodes"] = covertToString(poiTypeCodes)
            }
            
            let manager = AFHTTPRequestOperationManager()
            manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html","text/plain"]) as Set<NSObject>
            
            manager.GET(kURL+"favoritePOI", parameters: parameters, success: { (operation, responseObj) -> Void in
                
                let resultDic = responseObj as! [NSObject:AnyObject]
                
                let success = resultDic[kSuccess] as? Int
                let msg     = resultDic[kMsg] as? String
                
                if success == 0 {
                    
                    var poiList:[POIModel]?
                    
                    poiList <-- resultDic[kData]
                    
                    subscriber.sendNext(poiList)
                    subscriber.sendCompleted()
                    
                } else {
                    
                    subscriber.sendError(DataError.QueryPOIListByUserId(msg).error)
                }
                
                }) { (operation, error) -> Void in
                    
                    subscriber.sendError(DataError.QueryPOIListByUserId(nil).error)
            }
            
            return nil
        })
    }
    
    // MARK: - 查询指定目的地周边POI
    
    public func queryPOIListByCenterPOIId(pageIndex: Int, pageSize: Int, poiTypeCodes: [String]?, centerPoiId: String,poiType:Int) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber:RACSubscriber!) -> RACDisposable! in
            
            var parameters: [String:AnyObject] = ["pageIndex":pageIndex,"pageSize":pageSize,"poiId":centerPoiId,"poiType":poiType]
            
            if let poiTypeCodes=poiTypeCodes where poiTypeCodes.count != 0 {
                
                parameters["poiTypeCodes"] = covertToString(poiTypeCodes)
            }
            
            let manager = AFHTTPRequestOperationManager()
            manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html","text/plain"]) as Set<NSObject>
            
            manager.GET(kURL+"getAroundPoi", parameters: parameters, success: { (operation, responseObj) -> Void in
                
                let resultDic = responseObj as! [NSObject:AnyObject]
                
                let success = resultDic[kSuccess] as? Int
                let msg     = resultDic[kMsg] as? String
                
                if success == 0 {
                    
                    var poiList:[POIModel]?
                    
                    poiList <-- resultDic[kData]
                    
                    subscriber.sendNext(poiList)
                    subscriber.sendCompleted()
                    
                } else {
                    
                    subscriber.sendError(DataError.QueryPOIListByCenterPOIId(msg).error)
                }
                
            }) { (operation, error) -> Void in
                
                subscriber.sendError(DataError.QueryPOIListByCenterPOIId(nil).error)
            }
            
            return nil
        })
    }
    
    // MAKR: - 收藏/取消目的地
    
    public func collectPOI(poiId: String, userId: Int, isFavorite: Bool,poiType:Int) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber:RACSubscriber!) -> RACDisposable! in
            
            let parameters: [String:AnyObject] = [
                "poiId"    : poiId,
                "userId"   : userId,
                "isFavorite" : String(isFavorite),
                "poiType"  : poiType
            ]
            
            
            let manager = AFHTTPRequestOperationManager()
            manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html","text/plain"]) as Set<NSObject>
            
            manager.POST(kURL+"setFavoritePoi", parameters: parameters, success: { (operation, responseObj) -> Void in
                
                let resultDic = responseObj as! [NSObject:AnyObject]
                
                let success             = resultDic[kSuccess] as? Int
                let msg                 = resultDic[kMsg] as? String
                
                if success == 0 {
                    
                    subscriber.sendNext(msg)
                    subscriber.sendCompleted()
                    
                } else {
                    
                    subscriber.sendError(DataError.CollectPOIError(msg).error)
                }
                
            }) { (operation, error) -> Void in
                
                subscriber.sendError(DataError.CollectPOIError(nil).error)
            }
            
            return nil
        })
    }
    
    // MAKR: - 去过/取消目的地
    
    public func didVisitPOI(poiId: String, userId: Int, isTraveled:Bool,poiType:Int) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber:RACSubscriber!) -> RACDisposable! in
            
            let parameters: [String:AnyObject] = [
                "poiId"    : poiId,
                "userId"   : userId,
                "isTraveled" : String(isTraveled),
                "poiType"  : poiType
            ]
            
            let manager = AFHTTPRequestOperationManager()
            manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html","text/plain"]) as Set<NSObject>
            
            manager.POST(kURL+"setTraveledPoi", parameters: parameters, success: { (operation, responseObj) -> Void in
                
                let resultDic = responseObj as! [NSObject:AnyObject]
                
                let success             = resultDic[kSuccess] as? Int
                let msg                 = resultDic[kMsg] as? String
                
                if success == 0 {
                    
                    subscriber.sendNext(msg)
                    subscriber.sendCompleted()
                    
                } else {
                    
                    subscriber.sendError(DataError.DidVisitPOIError(msg).error)
                }
                
            }) { (operation, error) -> Void in
                
                subscriber.sendError(DataError.DidVisitPOIError(nil).error)
            }
            
            return nil
        })
    }
}
